<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .admin-message-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .send-message-btn {
    margin-bottom: 10px;
    margin-top: -46px;
    float: right;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>消息模板</span>
    </div>
    <div class="line"></div>
    <admin-message-form @on-success-valid="handleSubmit" class="admin-message-form" :title="title"></admin-message-form>
    <Button
      icon="md-add"
      type="primary"
      @click="handleCreate">
      创建模板
    </Button>
    <message-template-drawer :modalState="templateState" @on-cancel="cancelTemplate" @message-template-valid="handleEdit" :row="row"></message-template-drawer>
    <admin-message-drawer :modalState="modalState" @on-cancel="cancel" :row="row"></admin-message-drawer>
    <edit-confirm-modal :modalState="confirmModal" @cancel-confirm="cancelConfirm" @confirm-valid="handleConfirm"></edit-confirm-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import TablePage from '_c/table-page'
import AdminMessageForm from '_c/admin-message-form'
import AdminMessageDrawer from '_c/admin-message-drawer'
import MessageTemplateDrawer from '_c/message-template-drawer'
import EditConfirmModal from '_c/edit-confirm-modal'
export default {
  components: {
    AdminMessageForm,
    AdminMessageDrawer,
    MessageTemplateDrawer,
    EditConfirmModal,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '内容管理消息模板',
      confirmData: {},
      editData: null,
      playload: null,
      rows: 20,
      loading: true,
      modalState: false,
      templateState: false,
      confirmModal: false,
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '消息标题',
          key: 'title',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '内容',
          key: 'content',
          minWidth: 230,
          align: 'center',
          render: (h, params) => {
            if (params.row.content && params.row.content.length > 8) {
              return h('span', {
                attrs: {
                  title: params.row.content
                }
              }, params.row.content ? params.row.content.slice(0, 8) + '...' : '-')
            } else {
              return h('span', params.row.content ? params.row.content : '-')
            }
          }
        },
        {
          title: '创建日期',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        // {
        //   title: '操作人员',
        //   key: 'operater',
        //   minWidth: 150,
        //   align: 'center'
        // },
        // {
        //   title: '后缀内容',
        //   key: 'suffix',
        //   // width: 150,
        //   align: 'center'
        // },
        {
          title: '操作',
          key: 'cz',
          minWidth: 120,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row.id, params.row)
                  }
                }
              }, '查看')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      messageTemplateList: state => state.messageTemplate.messageTemplateList,
      dataCount: state => state.messageTemplate.messageTemplateDataCount,
      historyData: state => state.messageTemplate.messageTemplateHistoryData,
      pageSize: state => state.messageTemplate.messageTemplatePageSize,
      row: state => state.messageTemplate.row
    })
  },
  methods: {
    ...mapActions([
      'getMessageTemplateList',
      'getMessageTemplateCurrentRow',
      'editMessageTemplateData',
      'changeMessageTemplatePage',
      'changeMessageTemplatePageSize'
    ]),
    handleSubmit (playload) {
      this.loading = true
      this.playload = playload
      // console.log(playload)
      this.getMessageTemplateList({
        ...playload,
        type: 0,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleClick (id, row) {
      // console.log(row)
      let newRow = {}
      this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getMessageTemplateCurrentRow(newRow)
      this.modalState = true
    },
    handleEdit (playload) {
      this.editData = playload
      this.confirmModal = true
      this.templateState = false
    },
    cancel () {
      this.modalState = false
    },
    cancelTemplate (value) {
      this.templateState = false
      value.resetFields()
    },
    cancelConfirm () {
      this.confirmModal = false
      this.templateState = true
    },
    async handleConfirm () {
      this.loading = true
      this.confirmData = {
        title: this.editData.title,
        content: this.editData.content
      }
      const resp = await this.editMessageTemplateData(this.confirmData)
      if (resp.data.state === '000') {
        this.$Message.success('模板创建成功')
        this.getMessageTemplateList({
          page: this.page,
          rows: this.rows,
          type: 0,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.editData.drawer.resetFields()
      this.confirmModal = false
    },
    handleCreate () {
      this.templateState = true
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeMessageTemplatePage(index)
      this.getMessageTemplateList({
        page: index,
        rows: this.rows,
        type: 0,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeMessageTemplatePageSize(value)
      this.changePage(this.page)
      this.getMessageTemplateList({
        rows: value,
        type: 0,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getMessageTemplateList({
      type: 0
    }).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
